<?php

class Accounting extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		
		$this->load->model('Accounting_model');
	}
	
	function index()
	{
		$this->load->view('inc.header.php');
		$this->load->view('accounting/entries_view');
		$this->load->view('inc.footer.php');
	}
	
	function new_entry()
	{	
		$this->load->view('inc.header.php');
		$this->load->view('accounting/new_entry_view');
		$this->load->view('inc.footer.php');
	}
	
	function edit_entry($acctg_entry_id)
	{
		log_message('debug','edit_entry  acctg_entry_id : '. $acctg_entry_id);
		
		//Get the specified accounting entry
		$data = array();
		$data['acctg_entry'] = $this->Accounting_model->get_acctg_entries('', $acctg_entry_id);
		
		//Get work info to be displayed in the entry form
		$work_id = $data['acctg_entry'][$acctg_entry_id]['work_id'];
		log_message('debug','work_id : '. $work_id);
		if(!empty($work_id)) {
			$work_info = $this->Accounting_model->get_work_info_for_acctg_entry($work_id);
			$data['customer_po_num'] = $work_info['customer_po_num'];
			$data['customer_rfq_num'] = $work_info['customer_rfq_num'];
			$data['new_po_num'] = $work_info['new_po_num'];
			$data['new_rfq_num'] = $work_info['new_rfq_num'];
			$data['company_name'] = $work_info['company_name'];
		}
		
		
		
		//Get the account group
		$data['account_group'] = $this->Accounting_model->get_account_categories(true);
		
		//Get all specific accounts per account group
		$data['acctg_accounts'] = $this->Accounting_model->get_acctg_accounts(true);
		
		//Get all method of payments
		$data['payment_methods'] = $this->Accounting_model->get_acctg_payment_methods(true);
		
		//Get all payment method details per method
		$data['payment_method_details'] = $this->Accounting_model->get_payment_method_details_per_method();
		
		//Get all accounting receipts
		$data['acctg_receipts'] = $this->Accounting_model->get_acctg_receipts(true);
		log_message('debug','Get all accounting receipts');
		
		//Get all accounting receipt details per type
		$data['acctg_receipt_details'] = $this->Accounting_model->get_acctg_receipt_details_per_type();
		log_message('debug','Get all accounting receipt details : '. print_r($data['acctg_receipt_details'],true));
		
		//Get all transaction identifiers
		$data['transaction_identifiers'] = $this->Accounting_model->get_transaction_identifiers(true);
		
		$this->load->view('inc.header.php');
		$this->load->view('accounting/edit_entry_view', $data);
		$this->load->view('inc.footer.php');

		//Send result data
		//echo json_encode($data);
	}
	
	function entries()
	{
		//Get all accounting entries and display as initial list
		$data = array();
		$data['acctg_entries'] = $this->Accounting_model->get_acctg_entries();
		
		//Display view
		$this->load->view('inc.header.php');
		$this->load->view('accounting/entries_view', $data);
		$this->load->view('inc.footer.php');
	}
	
	function reports()
	{
		$this->load->view('inc.header.php');
		$this->load->view('accounting/report_view');
		$this->load->view('inc.footer.php');
	}
	
	function create_new_entry()
	{
		//Get json data
		$form_data = json_decode($this->input->post('json'));
		
		//Get the selected option
		$selected_option = $form_data->selected_option;
		$data['selected_option'] = $selected_option;
		if($selected_option == 'with_po_rfq')
		{
			//Get the PO # or RFQ #
			$po_rfq_num = $form_data->po_rfq_num;
			
			//Get work info to be displayed in the entry form
			$work_info = $this->Accounting_model->get_work_info_for_acctg_entry($po_rfq_num);
			$data['work_id'] = $work_info['work_id'];
			$data['customer_po_num'] = $work_info['customer_po_num'];
			$data['customer_rfq_num'] = $work_info['customer_rfq_num'];
			$data['new_po_num'] = $work_info['new_po_num'];
			$data['new_rfq_num'] = $work_info['new_rfq_num'];
			$data['description'] = $work_info['description'];
			$data['company_name'] = $work_info['company_name'];
		}
		else
		{
			$data['description'] = 'Enter the description for this entry.';
		}
		
		//Get the account group
		$data['account_group'] = $this->Accounting_model->get_account_categories(true);
		
		//Get all specific accounts per account group
		$data['acctg_accounts'] = $this->Accounting_model->get_acctg_accounts(true);
		
		//Get all method of payments
		$data['payment_methods'] = $this->Accounting_model->get_acctg_payment_methods(true);
		
		//Get all payment method details per method
		$data['payment_method_details'] = $this->Accounting_model->get_payment_method_details_per_method();
		
		//Get all accounting receipts
		$data['acctg_receipts'] = $this->Accounting_model->get_acctg_receipts(true);
		log_message('debug','Get all accounting receipts');
		
		//Get all accounting receipt details per type
		$data['acctg_receipt_details'] = $this->Accounting_model->get_acctg_receipt_details_per_type();
		log_message('debug','Get all accounting receipt details : '. print_r($data['acctg_receipt_details'],true));
		
		//Get all transaction identifiers
		$data['transaction_identifiers'] = $this->Accounting_model->get_transaction_identifiers(true);
		
		//log_message('debug','testing: ' . print_r($data,true));
		
		//Send result data
		echo json_encode($data);
	}
	
	function save_entry()
	{
		//Get json data
		$form_data = json_decode($this->input->post('json'));
		
		//Data	
		$data['work_id'] = $form_data->work_id;
		$data['description'] = $form_data->description;
		$data['acctg_account_id'] = $form_data->acctg_account_id;
		
		$transactions = $form_data->transactions;
		log_message('debug','harhar: '. print_r($form_data,true));
		
		$transaction_data = array();
		foreach($transactions as $transaction_id => $transaction_details) {
			$transaction_data[$transaction_id] = array();
			
			$transaction_data[$transaction_id]['transaction_date'] = $transaction_details->transaction_date;
			log_message('debug','testing transaction_date : '. $transaction_id .' = ' . $transaction_data[$transaction_id]['transaction_date']);
			
			if(strpos($transaction_id, 'MONEY_IN_') !== false ||
			   strpos($transaction_id, 'MONEY_OUT_') !== false) {
				   
				//Amount
				$transaction_data[$transaction_id]['amount_before_vat'] = $transaction_details->amount_before_vat;
				log_message('debug','testing amount_before_vat : '. $transaction_id .' = ' . $transaction_data[$transaction_id]['amount_before_vat']);
				
				$transaction_data[$transaction_id]['vat'] = $transaction_details->vat;
				log_message('debug','testing vat : '. $transaction_id .' = ' . $transaction_data[$transaction_id]['vat']);
				
				$transaction_data[$transaction_id]['withholding_tax'] = $transaction_details->withholding_tax;
				log_message('debug','testing withholding_tax : '. $transaction_id .' = ' . $transaction_data[$transaction_id]['withholding_tax']);
				
				$transaction_data[$transaction_id]['total_amount'] = $transaction_details->total_amount;
				log_message('debug','testing total_amount : '. $transaction_id .' = ' . $transaction_data[$transaction_id]['total_amount']);
				
				//Payment Method Details
				$payment_method_details = $transaction_details->payment_method_details;
				$transaction_data[$transaction_id]['payment_method_details'] = array();
				foreach($payment_method_details as $payment_method_detail_id => $value) {
					$transaction_data[$transaction_id]['payment_method_details'][$payment_method_detail_id] = $value;
					log_message('debug','testing payment_method_details : '. $payment_method_detail_id .' = ' . $value);
				}
			}
			
			//Receipt Details
			$acctg_receipt_details = $transaction_details->acctg_receipt_details;
			$transaction_data[$transaction_id]['acctg_receipt_details'] = array();
			foreach($acctg_receipt_details as $acctg_receipt_detail_id => $value) {
				$transaction_data[$transaction_id]['acctg_receipt_details'][$acctg_receipt_detail_id] = $value;
				log_message('debug','testing acctg_receipt_details : '. $acctg_receipt_detail_id .' = ' . $value);
			}
			
			//Attachments
			//log_message('debug','testing attachment : '. $transaction_details->attachment );
			//$original_filename = basename($_FILES[$transaction_details->attachment]['name']);
			//log_message('debug','testing attachment : '. print_r($_FILES,true));
			
			//Identifier
			$transaction_data[$transaction_id]['identifier_id'] = $transaction_details->identifier_id;
			log_message('debug','testing identifier : '. $transaction_id .' = ' . $transaction_data[$transaction_id]['identifier_id']);
			
			//Notes
			$transaction_data[$transaction_id]['notes'] = $transaction_details->notes;
			log_message('debug','testing note : '. $transaction_id .' = ' . $transaction_data[$transaction_id]['notes']);
		}
		
		$data['transactions'] = $transaction_data;
		
		//Add data and send back result
		echo $this->Accounting_model->add_acctg_entry($data);
	}
	
	function update_entry($acctg_entry_id)
	{
		//Get json data
		$form_data = json_decode($this->input->post('json'));
		
		//Data
		$data['acctg_entry_id']	= $acctg_entry_id;
		$data['work_id'] = $form_data->work_id;
		$data['description'] = $form_data->description;
		$data['acctg_account_id'] = $form_data->acctg_account_id;
		
		$transactions = $form_data->transactions;
		log_message('debug','harhar: '. print_r($form_data,true));
		
		$transaction_data = array();
		foreach($transactions as $transaction_id => $transaction_details) {
			$transaction_data[$transaction_id] = array();
			
			$transaction_data[$transaction_id]['transaction_date'] = $transaction_details->transaction_date;
			log_message('debug','testing transaction_date : '. $transaction_id .' = ' . $transaction_data[$transaction_id]['transaction_date']);
			
			if(strpos($transaction_id, 'MONEY_IN_') !== false ||
			   strpos($transaction_id, 'MONEY_OUT_') !== false) {
				   
				//Amount
				$transaction_data[$transaction_id]['amount_before_vat'] = $transaction_details->amount_before_vat;
				log_message('debug','testing amount_before_vat : '. $transaction_id .' = ' . $transaction_data[$transaction_id]['amount_before_vat']);
				
				$transaction_data[$transaction_id]['vat'] = $transaction_details->vat;
				log_message('debug','testing vat : '. $transaction_id .' = ' . $transaction_data[$transaction_id]['vat']);
				
				$transaction_data[$transaction_id]['withholding_tax'] = $transaction_details->withholding_tax;
				log_message('debug','testing withholding_tax : '. $transaction_id .' = ' . $transaction_data[$transaction_id]['withholding_tax']);
				
				$transaction_data[$transaction_id]['total_amount'] = $transaction_details->total_amount;
				log_message('debug','testing total_amount : '. $transaction_id .' = ' . $transaction_data[$transaction_id]['total_amount']);
				
				//Payment Method Details
				$payment_method_details = $transaction_details->payment_method_details;
				$transaction_data[$transaction_id]['payment_method_details'] = array();
				foreach($payment_method_details as $payment_method_detail_id => $value) {
					$transaction_data[$transaction_id]['payment_method_details'][$payment_method_detail_id] = $value;
					log_message('debug','testing payment_method_details : '. $payment_method_detail_id .' = ' . $value);
				}
			}
			
			//Receipt Details
			$acctg_receipt_details = $transaction_details->acctg_receipt_details;
			$transaction_data[$transaction_id]['acctg_receipt_details'] = array();
			foreach($acctg_receipt_details as $acctg_receipt_detail_id => $value) {
				$transaction_data[$transaction_id]['acctg_receipt_details'][$acctg_receipt_detail_id] = $value;
				log_message('debug','testing acctg_receipt_details : '. $acctg_receipt_detail_id .' = ' . $value);
			}
			
			//Attachments
			//log_message('debug','testing attachment : '. $transaction_details->attachment );
			//$original_filename = basename($_FILES[$transaction_details->attachment]['name']);
			//log_message('debug','testing attachment : '. print_r($_FILES,true));
			
			//Identifier
			$transaction_data[$transaction_id]['identifier_id'] = $transaction_details->identifier_id;
			log_message('debug','testing identifier : '. $transaction_id .' = ' . $transaction_data[$transaction_id]['identifier_id']);
			
			//Notes
			$transaction_data[$transaction_id]['notes'] = $transaction_details->notes;
			log_message('debug','testing note : '. $transaction_id .' = ' . $transaction_data[$transaction_id]['notes']);
		}
		
		$data['transactions'] = $transaction_data;
		
		//Add data and send back result
		echo $this->Accounting_model->update_acctg_entry($data);
	}
	
	function do_upload() {
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '100';
//		$config['max_width']  = '1024';
//		$config['max_height']  = '768';

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());

			$this->load->view('upload_form', $error);
		}
		else
		{
			$data = array('upload_data' => $this->upload->data());

			$this->load->view('upload_success', $data);
		}
	}
}
